<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:财务单据处理池
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class FundsmainAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
		$map['ordernumber|remarks'] = array('like',"%".$_POST['selectname']."%");
        if($_POST['ordertype']){
        $map['ordertype'] = $_POST['ordertype'];
        }
        $createU = $_SESSION[C('USER_AUTH_KEY')];
        if($createU!='1'){
         $map['createuser'] = $_POST['ordertype'];//如果非管理员则只能操作自己创建的单据
        }
        if($_POST['dealstatus']){
        $map['dealstatus'] = $_POST['dealstatus'];
        }else{
        $map['dealstatus'] = 0;   
        }
	 }
    
    
    //删除单据
    function delOrders(){        
       $arrData = $_REQUEST['ids'];
       $countArr = count($arrData);
       
       $mainModel = D('Fundsmain');
       $detailModel = D('Fundsdetails'); 
       $countDel = 0;
       
       for($i=0;$i<$countArr;$i++){
          $ids = '';
          $orderNumber ='';
          unset($where);
         if($arrData[$i]){
          $ids = $arrData[$i];
          $orderNumber = getTableColVal($ids,'Fundsmain','ordernumber');
          $where['ordernumber'] = $orderNumber;
          $LockStatus = $mainModel->where($where)->getField('status');
          if($LockStatus==0){
            $dealStatus = $mainModel->where($where)->getField('dealstatus');
             if($dealStatus==0){
               $mainModel->where($where)->delete();//删除总表信息
               $detailModel->where($where)->delete();//删除详细表信息
               $countDel=$countDel+1;
             }else{
                $this->error('发现单据:'.$orderNumber.'为已处理，本次处理过程终止。');exit;
             }             
          }else{
            $this->error('发现单据:'.$orderNumber.'已被锁定，本次处理过程终止。');exit;
          }
         }
       } 
       $this->success('成功删除 '.$countDel.' 个单据。');
   }  
   
   //处理单据
   function doDealOder(){
       $arrData = $_REQUEST['ids'];
       $countArr = count($arrData);
       
       $mainModel = D('Fundsmain');//初始化总单据表
       $banks = D('Bankaccount'); 
       $countDel = 0;
       for($i=0;$i<$countArr;$i++){
          $ids = '';
          $upAmount = 0;
          $orderNumber ='';
          $BankAmount = 0;
          $OrderAmount = 0;
          unset($where);
         if($arrData[$i]){
          $ids = $arrData[$i];//获取单据ID
          $orderNumber = getTableColVal($ids,'Fundsmain','ordernumber');//根据ID获取单据编号
          $where['ordernumber'] = $orderNumber;//设置查询单据编号条件
          $mainList = $mainModel->where($where)->find();//查询单据总信息         
          
          $bankid = $mainList['bankid'];//单据要操作的账户
          $mapBank['id'] = $bankid;//账户查询条件
          $BankAmount = $banks->where($mapBank)->getField('bamount');//查询帐号余额
          $OrderAmount = $mainList['trueamount'];//单据实际需付金额
                  
          //账户金额不足异常
          if($mainList['ordertype']==6){//判断付款单
            if(bccomp($BankAmount,$OrderAmount)<0 ){//账户余额小于付款额
              $this->error('账户余额不足,终止处理过程。');exit;  
            }
          }
          $LockStatus = ''; 
          $LockStatus = $mainModel->where($where)->getField('status');
          if($LockStatus==0){//是否单据被锁定
            $dealStatus='';
            $dealStatus = $mainModel->where($where)->getField('dealstatus');
             if($dealStatus==0){   //是否单据已处理过
                  if($mainList['ordertype']==7){
                      $upAmount = $BankAmount + $OrderAmount;//增加账户金额
                  }else if($mainList['ordertype']==6){
                      $upAmount = $BankAmount - $OrderAmount;//减少账户余额
                  }
                  if($banks->where($mapBank)->setField('bamount',$upAmount)){//如果更新成功
                    $countDel=$countDel+1;//处理数量递增
                    $mainModel->where($where)->setField('dealstatus',1);//设置单据状态为已处理
                    $mainModel->where($where)->setField('dealtime',strtotime(date('Y-m-d H:i:s')));//设置单据处理时间
                  }          
             }else{
                $this->error('发现单据:'.$orderNumber.'为已处理，本次处理过程终止。');exit;
             }             
          }else{
            $this->error('发现单据:'.$orderNumber.'已被锁定，本次处理过程终止。'.$OrderAmount);exit;
          }          
          
         }
       }
       if($countDel>0){
        $this->success('成功处理 '.$countDel.' 个单据。');
       }else{
        $this->error('处理过程异常。');
       } 
       
   }
   function printOrder(){
    $orderNumber = getTableColVal($_REQUEST['orderId'],'Fundsmain','ordernumber');
    
    $mainModel = D('Fundsmain');//初始化总单据表
    $detailModel = D('Fundsdetails');
    $NullM = M();        
    $map['ordernumber'] = $orderNumber;
    
    $mainList = $mainModel->where($map)->find();
    $detailSql = 'SELECT a . * , b.ordersort, b.costid, b.payamount AS pa2, b.remarks AS mk2
                    FROM `hp_fundsmain` AS a, hp_fundsdetails AS b
                    WHERE a.ordernumber = b.ordernumber
                    AND a.ordernumber =  \''.$orderNumber.'\'
                    ORDER BY ordersort';    
    $detailList = $NullM->query($detailSql);
   // $echoSql = $NullM->getLastSql();    
    //dump($echoSql);
    
    $this->assign ( 'detailList', $detailList);    
    $this->assign ( 'mainList', $mainList);
    $this->assign ( 'orderNumber', $orderNumber);
    
    $this->display();
   }
 
}
?>